<!-- 生产数据趋势图 -->
<script lang="ts" setup>
import MyChart from '@/components/MyChart.vue'
import { ref, watch } from 'vue'
const props = defineProps(['data'])
const maxValue = parseInt(props.data.max)
const minValue = parseInt(props.data.min)

const doData = (data:any) => {
  // 创建包含12个月的空数组
  const result = Array(10).fill('');

  // 填充实际数据
  data.forEach((item,index) => {
    result[index] = item
  });

  return result;
}

const options = ref({
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    top: '15%',
    containLabel: true
  },
  tooltip: {
    trigger: 'axis',
  },
  legend: {
    show: true,
    icon: 'circle',
    itemWidth: 15,
    itemHeight: 15,
    itemGap: 97,
    textStyle: {
      color: '#ebeef5',
      fontSize: 24
    },
    data: []
  },
  xAxis: [
    {
      type: 'category',
      data: [],
      boundaryGap: true,
      axisPointer: {
        type: 'shadow'
      },
      axisLabel: {
        color: '#ebeef5',
        fontSize: 18,
        interval: 0,
        margin: 10
      },
      axisTick: {
        show: false,
        alignWithLabel: false,
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: '#182758',
          width: 1
        }
      }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '',
      nameTextStyle: {
        color: 'rgba(255,255,255,0.85)',
        fontSize: 14,
        align: 'right',
        padding: [0, 5, 8, 0]
      },
      axisLabel: {
        formatter: '{value}',
        color: '#ebeef5',
        fontSize: 18,
        align: 'right'
      },
      axisLine: {
        show: false,
        lineStyle: {
          color: '#ff6600',
          width: 2
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: '#182758',
          width: 1
        }
      },
      max: function(value:any) {
        // value包含数据的最大最小值信息
        return Math.max(value.max, 100); // 取数据最大值和100中的较大者
      },
      data: [0, 2, 4, 5, 8, 10],
    },
  ],
  dataZoom: [{
    type: 'inside',
    filterMode: 'none'  // 不过滤极值点
  }],
  series: [
    {
      name: '',
      type: 'bar',
      color: '#0FC6C2',
      smooth: true,
      symbol: 'true',
      showSymbol: false,
      barWidth: 30,
      barGap: '0%', // 柱间无间隙
      barCategoryGap: '10px',
      itemStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: 'rgba(15, 198, 194, 0.7)' },
            { offset: 1, color: 'rgba(15, 198, 194, 0.1)' }
          ]
        }
      },
      label: {
        show: true, // 必须开启：显示数值
        position: 'top', // 位置：柱形顶部
        // 其他样式配置（可选）
        color: '#0FC6C2', // 数值颜色
        fontSize: 18, // 字号
        fontWeight: 'bold', // 字重
        formatter: '{c}', // 数值格式（{c} 表示数据值）
        distance: 5 // 与柱形顶部的距离（避免紧贴）
      },
      //   yAxisIndex: 1,
      tooltip: {
        valueFormatter: function (value: any) {
          return value + ''
        }
      },
      data: []
    },
    {
      name: '',
      type: 'line',
      color: '#1afa29',
      smooth: true,
      symbol: 'true',
      // showSymbol: false,
      label: {
        show: true,
        position: 'top',
        // position: 'outside', // 可能导致标签超出画布
        // overflow: 'break', // 允许标签溢出
        color: '#1afa29',
        fontSize: 12
      },
      lineStyle: {
        color: '#1afa29',
        width: 2,
        shadowColor: 'rgba(26, 250, 41, 0.3)',
        shadowBlur: 10,
        shadowOffsetY: 5
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            { offset: 0, color: 'rgba(26, 250, 41, 0.4)' },
            { offset: 1, color: 'rgba(26, 250, 41, 0)' }
          ]
        }
      },
      //   yAxisIndex: 1,
      tooltip: {
        valueFormatter: function (value: any) {
          return value + ''
        }
      },
      data: []

    }
  ]
})

watch(
  () => props.data,
  (val) => {
    const {legendList, timeList, targetList, actualList,max,min,title } = val
    options.value.xAxis[0].data = doData(timeList)
    options.value.series[0].data = actualList
    options.value.series[1].data = targetList
    // options.value.series[0].markLine.data[0].yAxis = min
    // options.value.series[0].markLine.data[1].yAxis = max

    if(actualList.length == 1 && actualList[0] == 0) {
      options.value.yAxis[0].min = 0;
      options.value.yAxis[0].max = 5;
      options.value.yAxis[0].interval = 1;
    }else if(title == 'little-bar'){
      options.value.yAxis[0].min = 0;
      options.value.yAxis[0].max = 10;
      options.value.yAxis[0].interval = 2;
    } else{
      options.value.yAxis[0].min = 0;
      options.value.yAxis[0].max = 100;
      options.value.yAxis[0].interval = 20;
    }
  },
  {
    deep: true, // 开启深度监听
    immediate: true // 立即触发一次（可选）
  }
)
</script>
<template>
  <MyChart height="160px" :option="options" />
</template>
